/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.front;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.entity.Article;
import com.mxg.blog.entity.ArticleCategory;
import com.mxg.blog.service.ArticleCategoryService;
import com.mxg.blog.service.ArticleService;
import com.mxg.blog.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/6/25 19:15
 * 前台获取文章
 */

@Controller
public class FrontGetArticles {
    CqLogger logger = CqLogger.getInstance();

    private final ArticleService articleService;
    private final UserService userService;
    private final ArticleCategoryService articleCategoryService;

    public FrontGetArticles(ArticleService articleService,
                            UserService userService,
                            ArticleCategoryService articleCategoryService) {
        this.articleService = articleService;
        this.userService = userService;
        this.articleCategoryService = articleCategoryService;
    }

    @GetMapping("api/getArticles")
    public void getArticles(HttpServletResponse response) throws Exception {
        List<Article> articleList = articleService.getArticleByUserIdAndStatus1(1);

        logger.print("前台获取文章");

        // 封装需要返回的数据
        List<Map<String, Object>> tableData = new ArrayList<>();

        for (Article article : articleList) {
            Map<String, Object> articleInfo = new HashMap<>();

            articleInfo.put(ResultCode.id, article.getArticleId());
            articleInfo.put(ResultCode.author, userService.selectUserById(1).getUserName());
            articleInfo.put(ResultCode.title, article.getArticleTitle());
            articleInfo.put(ResultCode.hero, article.getArticleImages());
            articleInfo.put(ResultCode.description, article.getArticleExcerpt());
            articleInfo.put(ResultCode.articleTags, article.getArticleTags());
            articleInfo.put(ResultCode.views, article.getArticleViews());
            articleInfo.put(ResultCode.update_at, article.getArticleUpdatedAt());
            tableData.add(articleInfo);
        }

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(tableData);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    @GetMapping("api/getArticleById")
    public void getArticlesById(@RequestParam String id, HttpServletResponse response) throws Exception {
        // logger.print(id);
        Integer articleId = new Integer(id);
        // logger.print(articleId);

        // 每一次搜索, views加一
        articleService.viewsPlusOne(articleId);

        // 封装文章数据
        // 获取数据
        Article article = articleService.getArticleByArticleId(articleId);
        //logger.print(article);
        Map<String, Object> tableData = new HashMap<>();

        tableData.put(ResultCode.id, article.getArticleId());
        tableData.put(ResultCode.author, userService.selectUserById(1).getUserName());
        tableData.put(ResultCode.title, article.getArticleTitle());
        tableData.put(ResultCode.hero, article.getArticleImages());
        tableData.put(ResultCode.description, article.getArticleExcerpt());
        tableData.put(ResultCode.articleTags, article.getArticleTags());
        tableData.put(ResultCode.views, article.getArticleViews());
        tableData.put(ResultCode.update_at, article.getArticleUpdatedAt());
        tableData.put(ResultCode.content, article.getArticleContentHtml());

        // 返回数据
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(tableData);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    @GetMapping("api/getArticleConById")
    public void getArticleConById(@RequestParam String id, HttpServletResponse response) throws Exception {
        // logger.print(id);
        Integer articleId = new Integer(id);
        // logger.print(articleId);

        // 每一次搜索, views加一
        articleService.viewsPlusOne(articleId);

        // 封装文章数据
        // 获取数据
        Article article = articleService.getArticleByArticleId(articleId);
        //logger.print(article);
        Map<String, Object> tableData = new HashMap<>();

        tableData.put(ResultCode.content, article.getArticleContentHtml());

        // 返回数据
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(tableData);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    @GetMapping("api/getArticleCategory")
    public void getArticleCategory(HttpServletResponse response) throws Exception{
        List<ArticleCategory> articleCategoryList = articleCategoryService.getArticleCategory();

        List<Map<String, Object>> tableData = new ArrayList<>();

        for (ArticleCategory articleCategory : articleCategoryList)
        {
            Map<String, Object> articleCategoryInfo = new HashMap<>();

            articleCategoryInfo.put(ResultCode.id, articleCategory.getArticleCategoryId());
            articleCategoryInfo.put(ResultCode.articleCategory, articleCategory.getArticleCategoryName());

            tableData.add(articleCategoryInfo);
        }

        logger.print(tableData);

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(tableData);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    @GetMapping("api/getArticlesByCategoryId")
    public void getArticlesByCategoryId(@RequestParam String id, HttpServletResponse response) throws Exception {
        // logger.print(id);
        Integer CategoryId = new Integer(id);
        // logger.print(articleId);

        // 封装文章数据
        // 获取数据
        List<Article> articleList = articleService.accordingToClassificationTheQueryStatus1(1, CategoryId);
        //logger.print(article);
        List<Map<String, Object>> tableData = new ArrayList<>();

        for(Article article : articleList) {
            Map<String, Object> articleInfo = new HashMap<>();

            articleInfo.put(ResultCode.id, article.getArticleId());
            articleInfo.put(ResultCode.author, userService.selectUserById(1).getUserName());
            articleInfo.put(ResultCode.title, article.getArticleTitle());
            articleInfo.put(ResultCode.hero, article.getArticleImages());
            articleInfo.put(ResultCode.description, article.getArticleExcerpt());
            articleInfo.put(ResultCode.articleTags, article.getArticleTags());
            articleInfo.put(ResultCode.views, article.getArticleViews());
            articleInfo.put(ResultCode.update_at, article.getArticleUpdatedAt());
            articleInfo.put(ResultCode.content, article.getArticleContentHtml());
            tableData.add(articleInfo);
        }
        // 返回数据
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(tableData);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }


}